/**
 * 
 */
package com.ccqlk.attendance.api.controller;

import java.sql.Time;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ccqlk.attendance.api.APIBaseController;
import com.ccqlk.attendance.model.AttendanceLog;
import com.ccqlk.attendance.model.OnlineUser;
import com.ccqlk.attendance.model.Staff;
import com.ccqlk.attendance.model.User;
import com.ccqlk.attendance.repository.service.AttendanceLogService;
import com.ccqlk.attendance.repository.service.OnlineUserService;
import com.ccqlk.attendance.repository.service.StaffService;
import com.ccqlk.attendance.repository.service.UserService;
import com.ccqlk.utils.RequestUtil;
import com.github.abel533.echarts.series.Map;
import com.google.common.collect.Maps;

/**
 * Copyright 2015-2018 重庆鲁控科技有限公司<br>
 * 文件标题: APIAttendanceLogController<br>
 * 文件说明: 出勤记录API<br>
 * 
 * @author ZhangYinxian 348893009@qq.com<br>
 *         创建时间 2016年5月27日 下午5:16:54<br>
 */
@RestController
@RequestMapping(APIBaseController.API_VERSION + "/attendance_log")
public class APIAttendanceLogController extends APIBaseController {

	private @Resource OnlineUserService onlineUserService;
	private @Resource AttendanceLogService attendanceLogService;
	
	private @Resource UserService userService;
	private @Resource StaffService staffService;
	
	@RequestMapping("add")
	public Model add(HttpServletRequest request, HttpServletResponse response, AttendanceLog attendanceLog, Model model, BindingResult result) {
        String token = RequestUtil.getValueByName(request, "token");
        List<OnlineUser> onlineUsers = onlineUserService.selectByToken(token);
        if (onlineUsers==null || onlineUsers.isEmpty()) {
			return renderFail(request, response, model, "token无效");
		}
        
        if (attendanceLog!=null) {
			attendanceLog.setUserCode(onlineUsers.get(0).getUserCode());
			attendanceLog.setADate(new Date());
			attendanceLog.setATime(Time.valueOf(LocalTime.now()));
			
			int count = attendanceLogService.add(attendanceLog);
			if (count==0) {
				return renderFail(request, response, model, "打卡失败");
			}
		}
        
		return renderSuccess(request, response, model);
	}

	/**
	 * 今天打卡的次数<br>
	 * <code>
	 * <b>请求地址:</b> API_VERSION/attendance_log/today_count.json<br>
	 * 请求参数：
	 * token: 令牌<br>
	 * address: 打卡地址<br>
	 * longitude: 经度<br>
	 * latitude: 维度<br>
	 * 
	 * </code>
	 * 
	 * @param request
	 *            请求
	 * @param response
	 *            响应
	 * @param model
	 *            模型
	 * @return count 今天打卡的次数
	 */
	@RequestMapping("today_count")
	public Model getCountByToday(HttpServletRequest request, HttpServletResponse response, Model model) {
        String token = RequestUtil.getValueByName(request, "token");
        String userCode = RequestUtil.getValueByName(request, "userCode");
        String staffCode = RequestUtil.getValueByName(request, "staffCode");
        
        List<OnlineUser> onlineUsers = onlineUserService.selectByToken(token);
        if (onlineUsers==null || onlineUsers.isEmpty()) {
			return renderFail(request, response, model, "token无效");
		}
        userCode = onlineUsers.get(0).getUserCode();
        int count = attendanceLogService.countByUserCode(userCode);
        
        java.util.Map<String, Object> params = Maps.newHashMap();
        params.put("code", userCode);
        
        List<User> users = userService.selectByParams(params);
        
        if (users!=null && users.size()>0) {
        	User user = users.get(0);
        	user.setPassword(null);
            model.addAttribute("user", user);
		}
        
        params.put("code", staffCode);
		
		List<Staff> staffs = staffService.selectByParams(params);
		if (staffs!=null && staffs.size()>0) {
			model.addAttribute("staff", staffs.get(0));
		}
        model.addAttribute("count", count);
        model.addAttribute("onlineUser", onlineUsers.get(0));
        
		return renderSuccess(request, response, model);
	}
}
